<?php 
namespace app\supermanager\controller;
use think\Validate;
use app\common\traits\controller\TraitController;
/**
 * 分公司管理类
 */
 class Filiale extends \think\Controller
{
	use TraitController;
	protected function init()
    {
    	if (!model('Managers')->checkLogin()) {
            $this->redirect('@manager/login');
            exit;
        }
        $this->model = model('Filiales');
		$this->list_route = url('/filiale');
    }
	
	/**
	 * 列表页
	 */
	public function index(){
		$list = $this->model->field('name,duty_person,mobile,email,id')->select();
		return view('filiale/index',['list'=>$list]);		
	}
	
	/**
	 * 添加表单
	 */
	public function create(){
		return view('filiale/create');		
	} 
	 
	/**
	 * 添加入库
	 */
	public function save(){
		foreach ($info = $this->request->post() as &$v) {
			$v = str_replace(' ', '', $v);
		}
		$result = $this->validate($info, 'Filiale');
		if(true !== $result){
		 	return $this->error($result);   
		}
		if($this->model->addInfo($info)){
			//发送账号密码到分公司
			$this->success('添加成功',$this->list_route);
		}
		$this->error('添加失败');
	} 
	
	/**
	 * 编辑表单
	 */
	public function edit($id){
		$info = $this->model->get($id);
	    return view('filiale/edit',['info'=>$info]);
	}
	
	/**
	 * 编辑入库
	 */
	public function update($id){
		foreach ($info = $this->request->post() as &$v) {
			$v = str_replace(' ', '', $v);
		}
		$result = $this->validate($info, 'Filiale');
		if(true !== $result){
		 	return $this->error($result);   
		}
		if($this->model->updateInfo($info,['id'=>$id])){
			$this->success('编辑成功',$this->list_route);
		}
	    $this->error('编辑失败');
	}
	
	/**
	 * 删除
	 */
	public function delete($id){
		if(model('Camps')->where('branch',$id)->find()){
			$this->error('分公司有关联的营地，不能删除');
		}
        if ($this->model->delInfo($id)) {
        	$this->success('删除成功', $this->list_route);
        }
        $this->error('删除失败');
	}
	
}

 ?>